package com.pingyougou.sellergoods.service.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationExample;
import com.pinyougou.pojo.TbSpecificationExample.Criteria;

import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.pojo.TbSpecificationOptionExample;
import com.pinyougou.pojogroup.Specification;
import com.pingyougou.sellergoods.service.SpecificationService;

import entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;
	@Autowired
	private TbSpecificationOptionMapper specificationOptionMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSpecification> page=   (Page<TbSpecification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 * 增加两个表的
	 */
	@Override
	public void add(Specification specification) {
		
		
		specificationMapper.insert(specification.getSpecification());
	
		List<TbSpecificationOption> specificationOptions = specification.getSpecificationOptionList();
		for(TbSpecificationOption option:specificationOptions) {
		option.setSpecId(specification.getSpecification().getId());//设置规格id
	    specificationOptionMapper.insert(option);//先曾规格
		}
		
	}

	
	
	/**
	 * 修改
	 */
	@Override
	public void update(Specification specification){
		
   specificationMapper.updateByPrimaryKey(specification.getSpecification());
   
   //先删掉在增加
       TbSpecificationOptionExample example=new TbSpecificationOptionExample();;
       TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
       criteria.andSpecIdEqualTo(specification.getSpecification().getId());
         specificationOptionMapper.deleteByExample(example);
      
		List<TbSpecificationOption> specificationOptions = specification.getSpecificationOptionList();
		for(TbSpecificationOption option:specificationOptions) {
			option.setSpecId(specification.getSpecification().getId());//设置规格id
			specificationOptionMapper.insert(option);//先曾规格
		}
		
		

	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Specification findOne(Long id){
			//查询规格
			Specification specification=new Specification();
			TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
				//获得实体
			specification.setSpecification(tbSpecification);
				
				//查询规格选项列表
				TbSpecificationOptionExample example=new TbSpecificationOptionExample();
				TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
				criteria.andSpecIdEqualTo(id);
				List<TbSpecificationOption> list = specificationOptionMapper.selectByExample(example);
				
				specification.setSpecificationOptionList(list);
				return specification;
		
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		//删除规格表id
		for(Long id:ids){
			specificationMapper.deleteByPrimaryKey(id);
			
			//同时删除规格选项表
			TbSpecificationOptionExample example=new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			
			specificationOptionMapper.deleteByExample(example);
			
		}		
	}
	
	
		@Override
	public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
	
		}
		
		Page<TbSpecification> page= (Page<TbSpecification>)specificationMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

		@Override
		public List<Map> selectOptionList() {
			// TODO Auto-generated method stub
			return specificationMapper.selectOptionList();
		}
	
}
